<?php

/**
 * Description of Group
 *
 * @author gun
 */

class Admin_Model_DbTable_Group extends Zend_Db_Table_Abstract {
    protected $_name = 'group';
    protected $_id = 'groupID';
    
    public function getGroup($where) {
        $sql = $this->getAdapter()->select()
                ->from($this->_name);
        
        if (is_array($where)) {
            foreach ($where as $key => $value) {
                $sql->where($key, $value);
            }
        }
        
        return $this->_db->fetchAll($sql);
    }
    
    public function getGroupById($id) {
        $sql = $this->getAdapter()->select()
                ->from($this->_name)
                ->where("$this->_id = ?", $id);
        
        return $this->_db->fetchRow($sql);
    }
    
    public function getGroupByName($name) {
        $sql = $this->getAdapter()->select()
                ->from($this->_name)
                ->where('name = ?', $name);        
        
        return $this->_db->fetchOne($sql);    
    }
    
    public function getGroupID() {
        $sql = $this->getAdapter()->select()
                ->from($this->_name, array('groupID'));
        
        return $this->_db->fetchAll($sql);
    }
    
    public function countRows($where) {
        $sql = $this->getAdapter()->select()
                ->from($this->_name, 'COUNT(groupID)');                
        
        if (is_array($where)) {
            foreach($where as $key=>$value) {
                $sql->where($key, $value);
            }
        }
        
        return $this->getAdapter()->fetchOne($sql);
    }
    
    public function doSave($data = array()) {
        return $this->getAdapter()->insert($this->_name, $data);
    }
    
    public function doUpdate($data = array(), $where = '') {
        return $this->getAdapter()->update($this->_name, $data, array("$this->_id = ?"=>$where));
    }
    
    public function doDelete($where) {
        return $this->getAdapter()->delete($this->_name, array("$this->_id = ?" => $where));
    }
}
